<?php
function calculate_shipping_cost() {
	// as we are shipping products all over the world
	// via teleportation, shipping is fixed
	return 20.00;
}

function get_categories() {
	// query database for a list of categories
	$conn = db_connect();
	$query = "select catid, catname from categories";
	$result = @$conn->query($query);
	if (!$result) {
		return false;
	}
	$num_cats = @$result->num_rows;
	if ($num_cats == 0) {
		return false;
	}
	$result = db_result_to_array($result);
	return $result;
}

function get_category_name($catid) {
	// query database for the name for a category id
	$conn = db_connect();
	$query = "select catname from categories
						where catid = '".$catid."'";
	$result = @$conn->query($query);
	if (!$result) {
		return false;
	}
	$num_cats = @$result->num_rows;
	if ($num_cats == 0) {
		return false;
	}
	$row = $result->fetch_object();
	return $row->catname;
}


function get_paperclips($catid) {
	// query database for the paperclips in a category
	if ((!$catid) || ($catid == '')) {
		return false;
	}

	$conn = db_connect();
	$query = "select * from paperclips where catid = '".$catid."'";
	$result = @$conn->query($query);
	if (!$result) {
		return false;
	}
	$num_paperclips = @$result->num_rows;
	if ($num_paperclips == 0) {
		return false;
	}
	$result = db_result_to_array($result);
	return $result;
}

function get_paperclip_details($sku) {
	// query database for all details for a particular paperclip
	if ((!$sku) || ($sku=='')) {
		return false;
	}
	$conn = db_connect();
	$query = "select * from paperclips where sku='".$sku."'";
	$result = @$conn->query($query);
	if (!$result) {
		return false;
	}
	$result = @$result->fetch_assoc();
	return $result;
}

function calculate_price($cart) {
	// sum total price for all items in shopping cart
	$price = 0.0;
	if(is_array($cart)) {
		$conn = db_connect();
		foreach($cart as $sku => $qty) {
			$query = "select price from paperclips where sku='".$sku."'";
			$result = $conn->query($query);
			if ($result) {
				$item = $result->fetch_object();
				$item_price = $item->price;
				$price +=$item_price*$qty;
			}
		}
	}
	return $price;
}

function calculate_items($cart) {
	// sum total items in shopping cart
	$items = 0;
	if(is_array($cart))	 {
		foreach($cart as $sku => $qty) {
			$items += $qty;
		}
	}
	return $items;
}
?>
